<template>
  <div>
    <el-row type="flex" justify="space-between" style="height: 40px" align="middle">
      <el-col>{{ data.name }}</el-col>
      <el-col>
        <el-row type="flex" justify="end">
          <el-col :span="8">{{ data.manager }}</el-col>
          <el-col :span="4">
            <el-dropdown @command="operateItem">
              <span class="el-dropdown-link">
                操作<i class="el-icon-arrow-down el-icon--right" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { delDepartment } from '@/api/department'
export default {
  props: {
    data: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    operateItem(type) {
      // 策略模式
      const operateMap = {
        add: () => {
          this.addDepart()
        },
        edit: () => {
          this.editDepart()
        },
        del: () => {
          this.delDepart()
        }
      }
      operateMap[type] ? operateMap[type]() : ''
      /* switch (type) {
        case 'add':
          this.addDepart()
          break
        case 'edit':
          this.editDepart()
          break
        case 'del':
          this.delDepart()
          break
        default:
          break
      } */
    },
    // 删除部门
    async delDepart() {
      await this.$confirm('确定是否删除部门？', {
        type: 'warning'
      })
      await delDepartment(this.data.id)
      // 通知父组件进行重新渲染页面
      this.$emit('delDepart-success')
      this.$message.success('删除部门成功')
    },
    // 添加子部门
    addDepart() {
      // 弹出弹框，并将当前的节点传递给父节点，用于添加子节点组件中添加到指定的节点
      this.$emit('addDepts', this.data)
    },
    // 编辑子部门
    editDepart() {
      // 弹出弹框，并将点击的当前子节点传递给父节点，让父节点获取当前点击的子节点的相关信息，进行表单填充
      this.$emit('editDepts', this.data)
    }
  }
}
</script>

<style>

</style>
